package cn.ygh.bbo.system.web.utils;

import cn.ygh.bbo.system.bean.Dept;
import cn.ygh.bbo.system.bean.Permission;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ygh
 * @Desc
 * @date 2019/11/19
 */
public class ConvertUtils {

    /**
     * 权限列表转成树结构
     *
     * @param list 分类表
     * @return String
     */
    public static List<Permission> listToTree(List<Permission> list) {
        //等级升序排序
        list.sort(Comparator.comparing(Permission::getLevel));
        List<Permission>result=new ArrayList<>();
        //初始化父类ID列表
        List<Integer>parentIds=new ArrayList<>();
        List<Permission>father=null;
        parentIds.add(0);
        while (true){
            //获得下一级菜单
            List<Integer> finalParentIds = parentIds;
            List<Permission>children=list.stream()
                    .filter(e-> finalParentIds.contains(e.getParentId())).collect(Collectors.toList());
            //子菜单没了结束
            if (children.size()==0){
                break;
            }
            parentIds=children.stream().map(Permission::getId).collect(Collectors.toList());
            //顶级的菜单
            if (father==null){
                result=children;
            }else {
                //子菜单循环嵌入
                for (Permission permission:father){
                    List<Permission>ps=children.stream()
                            .filter(e->e.getParentId().equals(permission.getId())).collect(Collectors.toList());
                    permission.setChildren(ps);
                }
            }
            //转成下级菜单
            father=children;
        }
        return result;
    }

    /**
     * 部门列表转成树结构
     *
     * @param list 分类表
     * @return String
     */
    public static List<Dept> listToTreeDept(List<Dept> list) {
        //等级排序
        list.sort(Comparator.comparing(Dept::getLevel));
        List<Dept>result=new ArrayList<>();
        //初始化父类ID列表
        List<Integer>parentIds=new ArrayList<>();
        List<Dept>father=null;
        parentIds.add(0);
        while (true){
            //获得下一级菜单
            List<Integer> finalParentIds = parentIds;
            List<Dept>children=list.stream()
                    .filter(e-> finalParentIds.contains(e.getParentId())).collect(Collectors.toList());
            //子菜单没了结束
            if (children.size()==0){
                break;
            }
            parentIds=children.stream().map(Dept::getId).collect(Collectors.toList());
            //顶级的菜单
            if (father==null){
                result=children;
            }else {
                //子菜单循环嵌入
                for (Dept dept:father){
                    List<Dept>ps=children.stream()
                            .filter(e->e.getParentId().equals(dept.getId())).collect(Collectors.toList());
                    dept.setChildren(ps);
                }
            }
            //转成下级菜单
            father=children;
        }
        return result;
    }
}
